package cn.tedu.mall.search.service.impl;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.pojo.product.model.Spu;
import cn.tedu.mall.pojo.search.entity.SpuForElastic;
import cn.tedu.mall.product.service.front.IForFrontSpuService;
import cn.tedu.mall.search.repository.SpuForElasticRepository;
import cn.tedu.mall.search.service.ISearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/*
@Service
@Slf4j
public class SearchServiceImpl  {

    // dubbo调用product模块分页查询所有spu信息的方法
    @DubboReference
    private IForFrontSpuService dubboSpuService;
    @Autowired
    private SpuForElasticRepository spuRepository;

    @Override
    public void loadSpuByPage() {
        int i=1;
        int pages;
        // 之所以用do while是因为业务场景符合先运行，后判断
        do {
            JsonPage<Spu> spus=dubboSpuService.getSpuByPage(i,2);
            List<SpuForElastic> list=new ArrayList<>();
            for(Spu spu : spus.getList()){
                SpuForElastic esSpu=new SpuForElastic();
                BeanUtils.copyProperties(spu, esSpu);
                list.add(esSpu);
            }
//            SpringESData提供的方法，功能是批量增
            spuRepository.saveAll(list);
            log.info("成功加载！第{}页信息",i);
            i++;
            pages=spus.getTotalPage();
        }while (i<=pages);

    }

    // 根据用户输入的关键字,分页搜索ES中spu信息
    @Override
    public JsonPage<SpuForElastic> search(String keyword, Integer page, Integer pageSize) {

        return null;
    }


}
*/
